package com.project.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.bean.ClassBean;
import com.project.bean.RoomBean;
import com.project.mapper.IClassMapper;
import com.project.mapper.IRoomMapper;
import com.project.service.IRoomService;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;

public class RoomServiceImp extends BaseService implements IRoomService {
    @Override
    public ArrayList<RoomBean> showAllRooms() {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        ArrayList<RoomBean> list = mapper.showAllRooms();
        session.close();
        return list;
    }

    @Override
    public ArrayList<RoomBean> findRoomByGender(String gender) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        ArrayList<RoomBean> list = mapper.findRoomByGender(gender);
        session.close();
        return list;
    }

    @Override
    public boolean addRoom(RoomBean roomBean) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        RoomBean room= mapper.RoomByAddress(roomBean.getRoomAddress());
        if(room == null){
            mapper.addRoom(roomBean);
            session.commit();
            session.close();
            return true;
        }
        session.close();
        return false;
    }

    @Override
    public boolean delRoom(Integer roomId) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        Integer num = mapper.roomInNumById(roomId);
        System.out.println(num);
        if(num == null || num ==0){
            mapper.delRoom(roomId);
            session.commit();
            session.close();
            return true;
        }
        return false;
    }

    @Override
    public RoomBean roomById(Integer roomId) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        RoomBean roomBean = mapper.roomById(roomId);
        session.close();
        return roomBean;
    }

    @Override
    public PageInfo<RoomBean> cutPageRoomByCondition(Integer pageNum, String roomAddress, String roomState, String roomType,String canInOrNo) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        PageHelper.startPage(pageNum,2);
        PageInfo<RoomBean> pg =PageInfo.of(mapper.cutPageRoomByCondition(roomAddress,roomState, roomType,canInOrNo));
        session.close();
        return pg;
    }


}
